package com.yc.UI;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

import com.yc.dao.UserDao;
import com.yc.utils.SWTUtil;
import com.yc.utils.StringUtil;
import com.yc.utils.Values;

/**
 *
 * @author UNO
 * 2021年6月28日下午1:32:07
 */
public class UserUpDateInfo {

	protected Shell shell;
	private boolean down = false;
	private int clickX = 0;
	private int clickY = 0;
	private Text text_1;
	private Text text_2;
	private Text text_4;
	private Text text_5;
	private byte[] photo;
	private Map<String, Object> map = Values.currentLoginUser;
	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			UserUpDateInfo window = new UserUpDateInfo();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell(SWT.NONE);
		shell.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		shell.setImage(SWTResourceManager.getImage(WelcomeUI.class, "/images/福字.png"));	
		shell.setSize(905, 507);
		shell.setLayout(new FillLayout(SWT.HORIZONTAL));

		Composite composite = new Composite(shell, SWT.NONE);
		composite.setBackgroundMode(SWT.INHERIT_FORCE);
		//背景图片
		composite.setBackgroundImage(SWTResourceManager.getImage(WelcomeUI.class, "/images/back.jpg"));
		// 设置窗口居中
		SWTUtil.centerWindom(shell);


		//设置图标
		Label label_Icon = new Label(composite, SWT.NONE);
		label_Icon.setBounds(0, 0, 40, 40);
		label_Icon.setImage(SWTResourceManager.getImage(WelcomeUI.class,"/images/福字.png"));

		//关闭按钮
		Label label_close = new Label(composite, SWT.NONE);

		label_close.setBounds(863, 0, 40, 40);
		label_close.setImage(SWTResourceManager.getImage(WelcomeUI.class,"/images/关闭.png"));

		//最小化按钮
		Label label_min = new Label(composite, SWT.NONE);
		label_min.setBounds(817, 0, 40, 40);
		label_min.setImage(SWTResourceManager.getImage(WelcomeUI.class,"/images/最小化.png"));

		Label label = new Label(composite, SWT.CENTER);
		label.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 10, SWT.NORMAL));
		label.setBounds(46, 10, 85, 30);
		label.setText("修改信息");

		//头像
		Label lb_photo = new Label(composite, SWT.BORDER);
		lb_photo.setBounds(25, 125, 150, 150);
		//传入默认头像
		Object obj = map.get("u_photo");
		if(obj == null) { //说明用户没有设置头像, 给他一个默认头像
			lb_photo.setImage(SWTResourceManager.getImage(UserInfoUI.class, "/images/默认头像.png"));
		}else {
			//把图片由Object类型转换成字节数组
			byte[] bt = (byte[]) obj;
			//把字节数组类型的图片 通过数据流 存入(转换成) imageData中
			ImageData imageData = new ImageData(new ByteArrayInputStream(bt));
			//图片自适应
			imageData = imageData.scaledTo(lb_photo.getSize().x, lb_photo.getSize().y);
			Image image = new Image(Display.getDefault(), imageData);
			lb_photo.setImage(image);
		}

		Label label_photo = new Label(composite, SWT.CENTER);
		label_photo.setBounds(25, 280, 129, 20);
		label_photo.setText("请选择您的头像");

		Label label_1 = new Label(composite, SWT.NONE);
		label_1.setBounds(240, 40, 76, 20);
		label_1.setText("姓名");

		Label label_2 = new Label(composite, SWT.NONE);
		label_2.setBounds(240, 120, 76, 20);
		label_2.setText("手机号");

		//用户的身份证号码，不可修改
		Label label_3 = new Label(composite, SWT.NONE);
		label_3.setBounds(240, 200, 76, 20);
		label_3.setText("身份证号码");

		Label label_4 = new Label(composite, SWT.NONE);
		label_4.setBounds(240, 280, 76, 20);
		label_4.setText("密码");

		Label label_6 = new Label(composite, SWT.NONE);
		label_6.setBounds(240, 360, 76, 20);
		label_6.setText("邮箱");


		//姓名
		text_1 = new Text(composite, SWT.BORDER);
		text_1.setBounds(434, 37, 215, 26);
		String account = map.get("u_name").toString();
		text_1.setText(account);

		//手机号
		text_2 = new Text(composite, SWT.BORDER);
		text_2.setBounds(434, 117, 215, 26);
		String tel = map.get("u_tel").toString();
		text_2.setText(tel);

		Label lb_cardid = new Label(composite, SWT.NONE);
		lb_cardid.setBounds(434, 199, 215, 20);
		String cardid = map.get("u_cardid").toString();
		lb_cardid.setText(cardid);

		//密码
		text_4 = new Text(composite,SWT.BORDER|SWT.PASSWORD);
		text_4.setBounds(434, 277, 215, 26);
		String pwd = map.get("u_pwd").toString();
		text_4.setText(pwd);

		//邮箱
		text_5 = new Text(composite, SWT.BORDER);
		text_5.setBounds(434, 357, 215, 26);
		String email = map.get("u_email").toString();
		text_5.setText(email);

		//账户
		Label Label_account = new Label(composite, SWT.NONE);
		Label_account.setBounds(444, 69, 150, 20);

		Button Button_update = new Button(composite, SWT.NONE);
		Button_update.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 14, SWT.NORMAL));
		Button_update.setBounds(337, 465, 98, 30);
		Button_update.setText("确认修改");

		Label Label_tel = new Label(composite, SWT.NONE);
		Label_tel.setBounds(444, 149, 150, 20);

		Label Label_cardid = new Label(composite, SWT.NONE);
		Label_cardid.setBounds(444, 227, 150, 20);

		Label Label_email = new Label(composite, SWT.NONE);
		Label_email.setBounds(444, 389, 150, 20);

		Label Label_pwd = new Label(composite, SWT.NONE);
		Label_pwd.setBounds(444, 314, 150, 20);

		//关闭按钮操作
		label_close.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				shell.dispose();
			}
		});
		//最小化操作
		label_min.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				shell.setMinimized(true);
			}
		});
		//面板拖动
		composite.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				down = true;
				clickX = e.x;
				clickY = e.y;
			}
			@Override
			public void mouseUp(MouseEvent e) {
				down = false;
			}
		});

		composite.addMouseMoveListener(new MouseMoveListener() {
			public void mouseMove(MouseEvent e) {
				if(down) {  //down为true，说明鼠标按下，窗口跟着鼠标移动			
					shell.setLocation(shell.getLocation().x + e.x - clickX, shell.getLocation().y + e.y - clickY);
				}
			}
		});
		text_1.setFocus();
		//账号回车向下走
		text_1.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if(e.keyCode == 13) {
					String txt = text_1.getText().trim();
					if(StringUtil.checkNull(txt)) {
						Label_account.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
						Label_account.setText("姓名不能为空...");
						text_1.setFocus();
						return;		
					} 		
					Label_account.setForeground(SWTResourceManager.getColor(SWT.COLOR_GREEN));
					Label_account.setText("");
					text_2.setFocus();
				}
			}
		});
		//手机号框监听事件
		text_2.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				Label_tel.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
				Label_tel.setText("请输入手机号...");
				if(e.keyCode == 13) {
					String txt = text_2.getText().trim();
					if(StringUtil.checkNull(txt)) {
						Label_tel.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
						Label_tel.setText("手机号不能为空...");
						text_2.setFocus();
						return;		
					} 		
					if( !(StringUtil.isInteger(txt)) || !(txt.length() == 11)) {
						Label_tel.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
						Label_tel.setText("手机号格式错误...");
						text_2.setFocus();
						return;
					}
					Label_tel.setForeground(SWTResourceManager.getColor(SWT.COLOR_GREEN));
					Label_tel.setText("手机号格式合格");
					text_4.setFocus();
				}
			}
		});

		//密码框回车向下走
		text_4.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if(e.keyCode == 13) {
					String str = text_4.getText().trim();
					if(StringUtil.checkNull(str)){
						Label_pwd.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
						Label_pwd.setText("密码不能为空...");
						return;
					}
					int i = str.length();
					int y = 0;
					if(i <= 6 && i > 0) {
						y = 0;
					}
					if(i > 6 && i <= 12) {
						y = 1;
					}
					if(i > 12) {
						y = 2;
					}

					switch(y) {
					case 0:
						Label_pwd.setForeground(SWTResourceManager.getColor(SWT.COLOR_GRAY));
						Label_pwd.setText("密码强度：弱");
						break;
					case 1:
						Label_pwd.setForeground(SWTResourceManager.getColor(SWT.COLOR_GRAY));
						Label_pwd.setText("密码强度：中等");
						break;
					default :
						Label_pwd.setForeground(SWTResourceManager.getColor(SWT.COLOR_GRAY));
						Label_pwd.setText("密码强度：强");
					}
					text_5.setFocus();
				}
			}
		});

		//邮箱回车向下走
		text_5.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if(e.keyCode == 13) {
					String str = text_5.getText().trim();				
					if(StringUtil.checkNull(str)) {
						Label_email.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
						Label_email.setText("邮箱不能为空...");
						text_5.setFocus();
						return;
					}
					//如果邮箱格式不正确
					if(!StringUtil.checkEmail(str)) {
						Label_email.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
						Label_email.setText("邮箱格式不正确...");
						return;
					}else {
						Label_email.setForeground(SWTResourceManager.getColor(SWT.COLOR_GREEN));
						Label_email.setText("邮箱格式合格...");
					}

				}
			}
		});
		Event event = new Event();
		event.widget = Button_update;

		//添加图片
		lb_photo.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				photo = null;
				FileDialog fd = new FileDialog(shell);
				fd.setFilterExtensions(new String[] {"*.jpg", "*.png", "*.gif"});
				fd.setText("选择头像");
				String path = fd.open();
				if(StringUtil.checkNull(path)) {
					return;
				}
				File fl = new File(path);
				try (InputStream is = new FileInputStream(fl)){
					photo = new byte[is.available()];
					is.read(photo);
					ImageData imageData = new ImageData(new ByteArrayInputStream(photo));
					//图片适应
					imageData = imageData.scaledTo(lb_photo.getSize().x, lb_photo.getSize().y);
					Image image = new Image(Display.getDefault(), imageData);
					lb_photo.setImage(image);
					label_photo.setText("");
				}catch(FileNotFoundException e1) {
					e1.printStackTrace();
				}catch(IOException e1){
					e1.printStackTrace();
				}
			}
		});

		//点击修改信息
		Button_update.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				//byte[] photo, String account, String pwd, String cardid, String tel, String email
				String account = text_1.getText().trim();
				String pwd = text_4.getText().trim();
				String cardid = lb_cardid.getText().trim();
				String tel = text_2.getText().trim();
				String email = text_5.getText().trim();

				if (StringUtil.checkNull( account, pwd, email, tel, cardid)) {
					SWTUtil.showMessage(shell, "提示", "信息不完整，无法修改！");
					return;
				}
				UserDao userdao = new UserDao();
				int i = userdao.upDateInfo(photo, account, pwd, tel, email, cardid);
				if (i > 0) {
					SWTUtil.showMessage(shell, "提示", "修改成功！");
					shell.dispose();
				}else {
					SWTUtil.showMessage(shell, "提示", "修改失败...");
				}
			}
		});

	}

}
